import variables from '@/framework/styles/element-variables.scss'
import defaultSettings from '@/framework/utils/settings'
import themeApi from '@/framework/api/themeManage'
import store from '@/store'
import { cloud } from '@/framework/utils/request'

const { recordUrl,
  recordNo,
  footerText,
  title,
  platformLogo,
  showSettings,
  tagsView,
  fixedHeader,
  sidebarLogo,
  logoAuto,
  openFooter,
  routerAnimation,
  navigationMode,
  sidebarTheme,
  TopTheme,
  homeBanner,
  helpEnter } = defaultSettings

const state = {
  theme: variables.theme, // 主题色
  showSettings,
  tagsView, // 多页签栏
  fixedHeader, // 固定导航栏
  sidebarLogo,
  logoAuto, // logo自适应
  openFooter, // 开启底栏
  navigationMode, // 导航模式
  sidebarTheme, // 侧边栏主题
  TopTheme, // 头部导航 颜色
  routerAnimation,
  platformName: title, // 平台名称
  platformLogo: '', // 平台logo
  footerText, // 页脚文字
  recordNo, // 备案号
  recordUrl, // 备案链接,
  dataText: ' ',
  appCode: null, // 当前应用appCode，
  homeBanner, // 门户banner
  helpEnter // 帮助中心入口 true/false
}

const mutations = {
  CHANGE_SETTING: (state, { key, value }) => {
    // eslint-disable-next-line no-prototype-builtins
    if (state.hasOwnProperty(key)) {
      state[key] = value
    }
  },
  SET_PLATFORMNAME: (state, platformName) => {
    state.platformName = platformName
  },
  SET_PLATFORMLOGO: (state, platformLogo) => {
    state.platformLogo = platformLogo
  },
  SET_HOMEBANNER: (state, homeBanner) => {
    state.homeBanner = homeBanner
  },
  SET_FOOTERTEXT: (state, footerText) => {
    state.footerText = footerText
  },
  SET_RECORDNO: (state, recordNo) => {
    state.recordNo = recordNo
  },
  SET_RECORDURL: (state, recordUrl) => {
    state.recordUrl = recordUrl
  },
  SET_NAVIGATIONMODE: (state, value) => {
    state.navigationMode = value
  },
  SET_LOGOAUTO: (state, value) => {
    state.logoAuto = value
  },
  SET_FIEXDHEADER: (state, value) => {
    state.fixedHeader = value
  },
  SET_APPCODE: (state, appCode) => {
    state.appCode = appCode
  },
  SET_HELPENTER: (state, value) => {
    state.helpEnter = value
  }
}

const actions = {
  changeSetting({ commit }, data) {
    commit('CHANGE_SETTING', data)
  },
  setAppCode({ commit }, appCode) {
    commit('SET_APPCODE', appCode)
  },
  // 获取主题配置的内容
  getCurrentThemeInfo({ commit }) {
    return new Promise((resolve, reject) => {
      themeApi.currentThemeInfo().then((response) => {
        const res = response.data
        commit('SET_PLATFORMNAME', res.platformName)
        if (res.platformLogo) {
          const platformLogo =
           [{ fileId: res.platformLogo, url: `${process.env.VUE_APP_BASE_API + cloud.file}/sysFileInfo/private/preview?fileId=${res.platformLogo}&token=${store.getters.token}`, name: '' }]
          commit('SET_PLATFORMLOGO', platformLogo[0].url)
        } else {
          commit('SET_PLATFORMLOGO', require(`@/${platformLogo}`))
        }
        if (res.homeBanner) {
          const homeBanner =
           [{ fileId: res.homeBanner, url: `${process.env.VUE_APP_BASE_API + cloud.file}/sysFileInfo/private/preview?fileId=${res.homeBanner}&token=${store.getters.token}`, name: '' }]
          commit('SET_HOMEBANNER', homeBanner[0].url)
        } else {
          commit('SET_HOMEBANNER', require(`@/${homeBanner}`))
        }
        commit('SET_FOOTERTEXT', res.footerText)
        commit('SET_RECORDNO', res.recordNo)
        commit('SET_RECORDURL', res.recordUrl)
        commit('SET_HELPENTER', res.helpEnter)

        const { routeSwitchingAnimation, topBar, sideBar, navigationMode, bottomBar, logoAdaptiveWidth,
          fixedNavigationBar, multiTabBar, themeColor } = response.data
        const themeObj = [
          {
            key: 'routerAnimation',
            value: routeSwitchingAnimation
          }, {
            key: 'TopTheme',
            value: topBar
          }, {
            key: 'sidebarTheme',
            value: sideBar
          }, {
            key: 'navigationMode',
            value: navigationMode
          }, {
            key: 'openFooter',
            value: bottomBar
          }, {
            key: 'logoAuto',
            value: logoAdaptiveWidth
          }, {
            key: 'fixedHeader',
            value: fixedNavigationBar
          }, {
            key: 'tagsView',
            value: multiTabBar
          }, {
            key: 'theme',
            value: themeColor || variables.theme
          }
        ]
        themeObj.forEach((item) => {
          commit('CHANGE_SETTING', item)
        })
        resolve(themeObj)
      }).catch((error) => {
        reject(error)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

